﻿using AJWPFAdmin.Core.Enums;
using AJWPFAdmin.Core.Logger;
using AJWPFAdmin.Core.Models.EF.Tables;
using AJWPFAdmin.Services.EF;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AJWPFAdmin.Services.AppointAPI.GoodsInfoQuery
{
    public enum GoodsInfoQueryTemplate
    {
        无,
        松汀,
        上海,
        AVS1,
        北京CEM
    }

    //public class CarGoodsInfo
    //{
    //    /// <summary>
    //    /// 编号,各自逻辑自行处理
    //    /// </summary>
    //    public string Code { get; set; }

    //    /// <summary>
    //    /// 货物名称
    //    /// </summary>
    //    public string Name { get; set; }

    //    /// <summary>
    //    /// 货物重量
    //    /// </summary>
    //    public string Weight { get; set; }

    //    /// <summary>
    //    /// 发货单位
    //    /// </summary>
    //    public string Sender { get; set; }

    //    /// <summary>
    //    /// 收货单位
    //    /// </summary>
    //    public string Receiver { get; set; }

    //    /// <summary>
    //    /// 收发标记
    //    /// </summary>
    //    public string SendOrReceiveFlag { get; set; }

    //    /// <summary>
    //    /// 物资类型
    //    /// </summary>
    //    public string GoodsType { get; set; }

    //    public GoodsInfoQueryBase QueryBase { get; set; }

    //    public void FillToCar(ref Car carInfo)
    //    {
    //        QueryBase?.FillGoodsInfo(ref carInfo, this);
    //    }
    //}

    public class GoodsInfoQueryBase : IDisposable
    {
        protected GoodsInfoQueryTemplate _name;
        protected DefaultAppointAPI.APIConfig _apiConfig;

        protected DbService _db;

        public GoodsInfoQueryBase(AJConfigService configSvc,
            DefaultAppointAPI.APIConfig apiConfig, GoodsInfoQueryTemplate name)
        {
            _name = name;
            _apiConfig = apiConfig;
            _db = DbService.GetDbContext(configSvc);
        }

        //todo-xq-查询货物名称填充
        public virtual void FillGoodsInfo(ref Car carInfo, WeighingDataCache dataCache)
        {
            if (dataCache == null)
            {
                return;
            }

            if (carInfo.SendOrReceiveFlag == PassagewayDirection.进.ToString())
            {
                carInfo.InGoodsName = string.IsNullOrWhiteSpace(dataCache.GoodsName)
                    ? carInfo.InGoodsName : dataCache.GoodsName;

                carInfo.InGoodsVolume = dataCache.GoodsWeight == 0
                    ? carInfo.InGoodsVolume : dataCache.GoodsWeight;

                carInfo.InGoodsType = string.IsNullOrWhiteSpace(dataCache.GoodsName)
                    ? carInfo.InGoodsType : dataCache.GoodsType;
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(dataCache.GoodsName) && dataCache.GoodsName != "空车")
                {
                    carInfo.OutGoodsName = dataCache.GoodsName;
                }

                carInfo.OutGoodsVolume = dataCache.GoodsWeight == 0
                    ? carInfo.OutGoodsVolume : dataCache.GoodsWeight;

                carInfo.OutGoodsType = string.IsNullOrWhiteSpace(dataCache.GoodsName)
                    ? carInfo.OutGoodsType : dataCache.GoodsType;
            }
        }

        public virtual Task<WeighingDataCache> QueryAsync(CarInfoParams carInfoParam)
        {
            return Task.FromResult(carInfoParam.CarInfo.WeighingCacheData);
        }

        public static GoodsInfoQueryBase Create(AJConfigService _configSvc
            , DefaultAppointAPI.APIConfig apiConfig,
            AJLog4NetLogger logger,
            GoodsInfoQueryTemplate name)
        {
            switch (name)
            {
                case GoodsInfoQueryTemplate.松汀:
                    return new SongTingGoodsInfoQuery(_configSvc, apiConfig, logger, name);
                case GoodsInfoQueryTemplate.上海:
                    return new ShangHaiGoodsInfoQuery(_configSvc, apiConfig, logger, name);
                case GoodsInfoQueryTemplate.AVS1:
                    return new AVS1GoodsInfoQuery(_configSvc, apiConfig, logger, name);
                case GoodsInfoQueryTemplate.北京CEM:
                    return new BeiJingCEMGoodsInfoQuery(_configSvc, apiConfig, logger, name);
                default:
                    return new GoodsInfoQueryBase(_configSvc, apiConfig, GoodsInfoQueryTemplate.无);
            }
        }

        public void Dispose()
        {
            _db?.Dispose();
        }
    }
}
